// TypeScript类型定义 - 基于629条消息的分析结果
// 生成时间: 2025-06-20
// 分析了8种不同的消息类型

// 基础图标接口
interface IconInfo {
  height: string;
  imageType: number;
  urlList: string[];
  width: string;
  uri?: string; // 在某些消息类型中存在
  isAnimated?: boolean; // 极少数情况下存在
}

// 用户等级信息
interface PayGrade {
  level?: string; // 89.7% 在 MemberMessage 中出现
  newImIconWithLevel?: IconInfo; // 89.4% 在 MemberMessage 中出现
  newLiveIcon?: IconInfo; // 89.7% 在 MemberMessage 中出现
}

// 粉丝团数据
interface FansClubData {
  badge: object; // 总是存在但结构未详细分析
  anchorId?: string; // 可选字段，仅粉丝团成员有
  level?: number; // 可选字段，仅粉丝团成员有
  userFansClubStatus?: number; // 可选字段，仅粉丝团成员有
}

// 粉丝团信息
interface FansClub {
  data: FansClubData;
}

// 关注信息
interface FollowInfo {
  followingCount: string; // 几乎总是存在 (99.7%)
  followerCount?: string; // 大部分情况存在 (92-96%)
  followStatus?: string; // 可选字段 (31-63%)
}

// 基础用户信息
interface BaseUser {
  id: string; // 必须
  name: string; // 必须
  avatar: string; // 必须
  displayId: string; // 必须
  shortId: string; // 必须
  fansClub: FansClub; // 必须
  followInfo: FollowInfo; // 必须
  payGrade: PayGrade; // 必须
  badges?: string[]; // 常见字段 (75-89%)
  gender?: number; // 常见字段 (55-84%)
  level?: number; // 可选字段 (3-29%)
  // 管理员权限字段
  isMuted?: boolean; // 是否被禁言
  isAdmin?: boolean; // 是否为管理员
  isSuperAdmin?: boolean; // 是否为超级管理员
  adminPrivileges?: number[]; // 管理员权限列表
}

// 房间信息 - 成员消息
interface MemberRoom {
  audienceCount: string;
}

// 房间信息 - 点赞消息
interface LikeRoom {
  likeCount: string;
}

// 房间信息 - 用户序列消息
interface UserSeqRoom {
  audienceCount: string;
  totalUserCount: string;
}

// 房间信息 - 统计消息
interface StatsRoom {
  audienceCount: string;
}

// 房间信息 - 社交消息
interface SocialRoom {
  // 结构未在分析中详细展示，但存在
  [key: string]: any;
}

// 礼物信息
interface Gift {
  id: string;
  name: string;
  desc: string;
  icon: string;
  price: number;
  type: number;
  count: string;
  repeatEnd?: number; // 可选字段 (44.4%)
}

// RTF内容（富文本）
interface RtfContent {
  // 结构未详细分析，但在10.2%的聊天消息中存在
  [key: string]: any;
}

// 基础消息接口
interface BaseMessage {
  id: string;
  method: string;
}

// 1. 成员进入消息 (WebcastMemberMessage) - 358条
export interface WebcastMemberMessage extends BaseMessage {
  method: 'WebcastMemberMessage';
  user: BaseUser;
  content: string; // 通常是 "进入直播间"
  room: MemberRoom;
}

// 2. 聊天消息 (WebcastChatMessage) - 88条
export interface WebcastChatMessage extends BaseMessage {
  method: 'WebcastChatMessage';
  user: BaseUser;
  content: string;
  rtfContent?: RtfContent[]; // 可选字段 (10.2%)
}

// 3. 房间用户序列消息 (WebcastRoomUserSeqMessage) - 56条
export interface WebcastRoomUserSeqMessage extends BaseMessage {
  method: 'WebcastRoomUserSeqMessage';
  rank: {
    avatar: string,
    nickname: string,
    rank: number,
  }[]; // 用户排行数组
  room: UserSeqRoom;
}

// 4. 房间统计消息 (WebcastRoomStatsMessage) - 53条
export interface WebcastRoomStatsMessage extends BaseMessage {
  method: 'WebcastRoomStatsMessage';
  room: StatsRoom;
}

// 5. 点赞消息 (WebcastLikeMessage) - 52条
export interface WebcastLikeMessage extends BaseMessage {
  method: 'WebcastLikeMessage';
  user: BaseUser;
  content: string; // 通常是 "为主播点赞了(N)"
  room: LikeRoom;
}

// 6. 礼物消息 (WebcastGiftMessage) - 9条
export interface WebcastGiftMessage extends BaseMessage {
  method: 'WebcastGiftMessage';
  user: BaseUser;
  gift: Gift;
}

// 7. 社交消息 (WebcastSocialMessage) - 7条
export interface WebcastSocialMessage extends BaseMessage {
  method: 'WebcastSocialMessage';
  user: BaseUser;
  content: string;
  room: SocialRoom;
}

// 8. 房间排行消息 (WebcastRoomRankMessage) - 6条
export interface WebcastRoomRankMessage extends BaseMessage {
  method: 'WebcastRoomRankMessage';
  // 具体字段结构需要进一步分析
  rank: {
    avatar: string,
    nickname: string,
    rank: number,
  }[];
}

// 联合类型 - 所有消息类型
export type LiveStreamMessage =
  | WebcastMemberMessage
  | WebcastChatMessage
  | WebcastRoomUserSeqMessage
  | WebcastRoomStatsMessage
  | WebcastLikeMessage
  | WebcastGiftMessage
  | WebcastSocialMessage
  | WebcastRoomRankMessage;

// 消息包装器 - 包含时间戳和房间ID
export interface MessageWrapper {
  timestamp: string;
  roomId: string;
  messages: LiveStreamMessage[];
}

// 消息类型枚举
export enum MessageType {
  MEMBER = 'WebcastMemberMessage',
  CHAT = 'WebcastChatMessage',
  ROOM_USER_SEQ = 'WebcastRoomUserSeqMessage',
  ROOM_STATS = 'WebcastRoomStatsMessage',
  LIKE = 'WebcastLikeMessage',
  GIFT = 'WebcastGiftMessage',
  SOCIAL = 'WebcastSocialMessage',
  ROOM_RANK = 'WebcastRoomRankMessage',
}

// 类型守卫函数
export function isWebcastMemberMessage(message: LiveStreamMessage): message is WebcastMemberMessage {
  return message.method === 'WebcastMemberMessage';
}

export function isWebcastChatMessage(message: LiveStreamMessage): message is WebcastChatMessage {
  return message.method === 'WebcastChatMessage';
}

export function isWebcastGiftMessage(message: LiveStreamMessage): message is WebcastGiftMessage {
  return message.method === 'WebcastGiftMessage';
}

export function isWebcastLikeMessage(message: LiveStreamMessage): message is WebcastLikeMessage {
  return message.method === 'WebcastLikeMessage';
}

export function isWebcastRoomUserSeqMessage(message: LiveStreamMessage): message is WebcastRoomUserSeqMessage {
  return message.method === 'WebcastRoomUserSeqMessage';
}

export function isWebcastRoomStatsMessage(message: LiveStreamMessage): message is WebcastRoomStatsMessage {
  return message.method === 'WebcastRoomStatsMessage';
}

export function isWebcastSocialMessage(message: LiveStreamMessage): message is WebcastSocialMessage {
  return message.method === 'WebcastSocialMessage';
}

export function isWebcastRoomRankMessage(message: LiveStreamMessage): message is WebcastRoomRankMessage {
  return message.method === 'WebcastRoomRankMessage';
}

// 字段验证配置
export const REQUIRED_FIELDS = {
  [MessageType.MEMBER]: [
    'id', 'method', 'user.id', 'user.name', 'user.avatar',
    'user.displayId', 'user.shortId', 'content', 'room.audienceCount'
  ],
  [MessageType.CHAT]: [
    'id', 'method', 'user.id', 'user.name', 'user.avatar',
    'user.displayId', 'user.shortId', 'content'
  ],
  [MessageType.GIFT]: [
    'id', 'method', 'user.id', 'user.name', 'user.avatar',
    'gift.id', 'gift.name', 'gift.price', 'gift.count'
  ],
  [MessageType.LIKE]: [
    'id', 'method', 'user.id', 'user.name', 'user.avatar',
    'content', 'room.likeCount'
  ],
  [MessageType.ROOM_USER_SEQ]: [
    'id', 'method', 'rank', 'room.audienceCount', 'room.totalUserCount'
  ],
  [MessageType.ROOM_STATS]: [
    'id', 'method', 'room.audienceCount'
  ],
  [MessageType.SOCIAL]: [
    'id', 'method', 'user.id', 'user.name', 'content'
  ],
  [MessageType.ROOM_RANK]: [
    'id', 'method'
  ]
} as const;